package com.example.dao;

import com.example.entity.BackendApi;
import com.example.entity.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface RoleDao {

    @Select("SELECT * FROM roles")
    List<Role> getRoles();

    @Select("SELECT * FROM roles WHERE id = #{id}")
    Role getRoleById(int id);

    //根据角色id查询api
    @Select("SELECT api_id FROM role_backend_apis t1 " +
            "WHERE role_id = #{roleId} AND can_access=1")
    List<Integer> getApisByRoleId(int roleId);

    @Update("UPDATE roles SET name = #{name}, description = #{description} WHERE id = #{id}")
    void updateRole(Role role);

    @Insert("INSERT INTO roles (name, description) VALUES (#{name}, #{description})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    void addRole(Role role);

    @Delete("DELETE FROM role_backend_apis WHERE role_id = #{roleId}")
    void deleteApiByRoleId(int roleId);

    @Insert("INSERT INTO role_backend_apis (role_id, api_id) VALUES (#{roleId}, #{apiId})")
    void addApiByRoleId(@Param("roleId") int roleId,@Param("apiId") int apiId);

}
